package com.yiran.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

public class EncryptUtils {
    public static final int SALT_LENGTH=16;
    public static final String HASH_ALGORITHM = "SHA-256";

    //生成盐
    public static String getSalt(){
        SecureRandom secureRandom = new SecureRandom();
        byte[] salt = new byte[SALT_LENGTH];
        secureRandom.nextBytes(salt);
        String s = Base64.getEncoder().encodeToString(salt);
        return s;
    }
    // 加密
    public static String encrypt(String salt,String password) throws NoSuchAlgorithmException {
        String newPassWord=password+salt;
        MessageDigest instance = MessageDigest.getInstance(HASH_ALGORITHM);
        byte[] digest = instance.digest(newPassWord.getBytes(StandardCharsets.UTF_8));
        String s = Base64.getEncoder().encodeToString(digest);
        return s;
    }
}
